<?php

include('pagina.php');
include('database.php');
include('auth.php');
include('core.php');

// Verifica se esta autenticado com pelo menos nivel 3
validaPermissao(3);

// Verifica se houve envio do formulario.
if (isset($_POST['submit']))
{
	// Cria contato
	query_bd("INSERT INTO contacts VALUES " .
	"(NULL, " . ($_POST['name']? "'{$_POST['name']}'" : "NULL") . 
	", " . ($_POST['number']? "{$_POST['number']}" : "NULL") . 
	", " . ($_POST['street']? "'{$_POST['street']}'" : "NULL") . 
	", " . ($_POST['complement']? "'{$_POST['complement']}'" : "NULL") .
	", " . ($_POST['city']? "'{$_POST['city']}'" : "NULL") . 
	", " . ($_POST['state']? "'{$_POST['state']}'" : "NULL") . 
	", " . ($_POST['zipcode']? "'{$_POST['zipcode']}'" : "NULL") . 
	", " . ($_POST['phone']? "'{$_POST['phone']}'" : "NULL") .
	", " . ($_POST['cellphone']? "'{$_POST['cellphone']}'" : "NULL") . 
	", " . ($_POST['email']? "'{$_POST['email']}'" : "NULL") . ")");
	
	$contact_id = mysql_insert_id(); //pega ultimo id inserido
	
	// Cria employee
	query_bd("INSERT INTO customers VALUES " . 
	"(NULL, {$contact_id}, ". ($_POST['obs']? "'" . 
	mysql_real_escape_string($_POST['obs']) . "'" : "NULL") . ")");
	
	$customer_id = mysql_insert_id(); //pega ultimo id inserido
	
	header( "Location: customer.php?id={$customer_id}&msg=New customer created." );
	exit;
}

?>
<script type="text/javascript" language="JavaScript">

function checkForm()
{

	if (document.customer_new.name.value.length == 0)
	{
		alert("The Name field must not be empty.");
		return false;
	}
	else
		return true;
}

</script>

<?php

// Inicia cabecalho da pagina
pagina_inicio('ParkSys - New Customer');
pagina_head();

echo "<form name=customer_new class=big_form method='POST' action='customer_new.php' onSubmit='return checkForm()'>";
echo "<h1>New Customer</h1><br>";
echo "<label for=name>Name:</label> <input type=TEXT id=name name=name value='' size=40 maxlength=80 /><br>";
echo "<hr><h2>Contact</h2><br>";
echo "<label for=number>Number:</label> <input type=TEXT id=number name=number value='' size=8 onChange='format_int(this)' />";
echo " <label for=street>Street:</label> <input type=TEXT id=street name=street value='' size=30 maxlength=50 />";
echo " <label for=complement>Complement:</label> <input type=TEXT id=complement name=complement value='' size=10 maxlength=30 /><br>";
echo "<label for=ciy>City:</label> <input type=TEXT id=city name=city value='' size=10 maxlength=30 />";
echo " <label for=state>State:</label> <input type=TEXT id=state name=state value='' size=8 maxlength=10 />";
echo " <label for=zipcode>Zipcode:</label> <input type=TEXT id=zipcode name=zipcode value='' size=6 maxlength=10 /><br>";
echo "<label for=phone>Phone:</label> <input type=TEXT id=phone name=phone value='' size=12 maxlength=20 />";
echo " <label for=cellphone>Cellphone:</label> <input type=TEXT id=cellphone name=cellphone value='' size=12 maxlength=20 /><br>";
echo "<label for=email>Email:</label> <input type=TEXT id=email name=email value='' size=20 maxlength=30 />";
echo "<hr>";
echo "<label for=obs>Observations:</label><br><textarea id=obs name=obs cols=40 rows=5 />";
echo "</textarea>";
echo "<hr>";
echo "<input type=SUBMIT name=submit value='Submit' />";
echo "</form>";

// Finaliza pagina
pagina_fim();
?>